package com.itheima.service;

import com.itheima.common.JsonResult;
import com.itheima.common.SnowFlake;
import com.itheima.mapper.admin.CategoryMapper;
import com.itheima.mapper.admin.DishMapper;
import com.itheima.mapper.admin.SetmealMapper;
import com.itheima.pojo.admin.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ChenJunJie
 * @date 2022/6/13 16:21
 * 分类service
 */
@Service
public class SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SnowFlake snowFlake;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishMapper dishMapper;


    //新增套餐
    public JsonResult<String> setmealAdd(Setmeal<SetmealDish> setmeal) {

        //设置套餐id
        long id = snowFlake.nextId();
        setmeal.setId(id + "");

        //设置套餐所属分类
        Category category = categoryMapper.categorySelectById(setmeal.getCategoryId());
        setmeal.setType(category.getType());

        int setmealNum = setmealMapper.setmealAdd(setmeal);

        for (SetmealDish setmealDish : setmeal.getSetmealDishes()) {

            //设置套餐菜品id
            long setmealDishesId = snowFlake.nextId();
            setmealDish.setId(setmealDishesId + "");

            setmealMapper.setmealRelationAdd(setmeal.getId(), setmealDish);
        }

        if (setmeal.getSetmealDishes().size() <= 0) {
            return JsonResult.success("新增套餐菜品失败,请选择套餐的菜品");
        }

        if (setmealNum > 0) {
            return JsonResult.success("新增套餐成功");
        } else {
            return JsonResult.error("新增套餐失败");
        }
    }

    //套餐分页查询
    public JsonResult<Pagination<Setmeal<SetmealDish>>> setmealPagination(String name, Integer page, Integer pageSize) {

        Integer index = (page - 1) * pageSize;

        String newName = name;
        if (name != null) {
            newName = "%" + name + "%";
        }


        List<Setmeal<SetmealDish>> setmeals = setmealMapper.setmealPagination(newName, index, pageSize);

        List<Setmeal<SetmealDish>> setmealsNum = setmealMapper.setmealSelectByName(newName);

        //获取分类名称并放到setmeal对象中
        for (Setmeal<SetmealDish> setmeal : setmeals) {
            Category category = categoryMapper.categorySelectById(setmeal.getCategoryId());
            setmeal.setCategoryName(category.getName());
        }




        Pagination<Setmeal<SetmealDish>> eps = new Pagination<Setmeal<SetmealDish>>();
        eps.setRecords(setmeals);
        eps.setTotal(setmealsNum.size());
        eps.setSize(pageSize);
        eps.setCurrent(page);

        if (setmeals.size() <= 0 || setmealsNum.size() <= 0) {
            return JsonResult.employeePaginationSuccess(eps,"查询失败");
        }
        return JsonResult.employeePaginationSuccess(eps, "查询成功");
    }

    //通过id查询套餐
    public JsonResult<Setmeal<SetmealDish>> setmealSelectById(String id) {

        //获取套餐信息
        Setmeal<SetmealDish> dishSetmeal = setmealMapper.setmealSelectById(id);

        //获取套餐与菜品的关系
        List<SetmealRelation> setmealRelations = setmealMapper.setmealRelationSelectById(id);

        //创建集合接收菜品
        List<SetmealDish> list = new ArrayList<>();

        //将菜品的放入集合中
        for (SetmealRelation setmealRelation : setmealRelations) {
            Dish<FlavorsById> flavorsByIdDish = dishMapper.dishSelectById(setmealRelation.getDishId());
            SetmealDish sd = new SetmealDish();

            long sdId = snowFlake.nextId();
            sd.setId(sdId + "");
            sd.setDishId(flavorsByIdDish.getId());
            sd.setName(flavorsByIdDish.getName());
            sd.setPrice(flavorsByIdDish.getPrice());
            sd.setPrice(flavorsByIdDish.getPrice());
            sd.setCopies(setmealRelation.getCopies());
            sd.setCreateTime(flavorsByIdDish.getCreateTime());
            sd.setUpdateTime(flavorsByIdDish.getUpdateTime());
            sd.setCreateUser(flavorsByIdDish.getCreateUser());
            sd.setUpdateUser(flavorsByIdDish.getUpdateUser());
            list.add(sd);
        }
        //将菜品集合放入套餐信息中
        if (dishSetmeal != null) {
            dishSetmeal.setSetmealDishes(list);
            return JsonResult.select(dishSetmeal, "查询成功");
        } else {
            return JsonResult.error("查询失败");
        }
    }

    //通过套餐id修改套餐
    public JsonResult<String> setmealUpdateById(Setmeal<SetmealDish> setmeal) {


        //修改套餐基本信息
        int setmealNum = setmealMapper.setmealUpdateById(setmeal);

        //删除套餐下绑定的菜品
        int dishDeleteNum = setmealMapper.setmealDishDeleteById(setmeal.getId());

        //绑定前端传输的菜品id
        int dishUpdateNum = 0;
        List<SetmealDish> setmealDishes = setmeal.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            long id = snowFlake.nextId();
            setmealDish.setId(id + "");
            dishUpdateNum = setmealMapper.setmealRelationAdd(setmeal.getId(), setmealDish);
        }

        if (setmealNum <= 0) {
            return JsonResult.error("修改套餐失败");
        }

        if (dishUpdateNum <= 0) {
            return JsonResult.error("修改套餐菜品失败，套餐菜品不能为空");
        }

        return JsonResult.success("修改套餐成功");
    }

    //通过套餐id删除套餐
    public boolean setmealDeleteById(String id) {

        String[] split = id.split(",");



        int count = 0;
        for (String s : split) {
            //删除套餐对应的菜品关系
            int setmealDishNum = setmealMapper.setmealDishDeleteById(s);
            if (setmealDishNum<=0){
                return false;
            }

            int i = setmealMapper.setmealDeleteById(s);
            count=count+i;
        }

        return count == split.length;
    }

    //通过套餐id修改套餐状态
    public boolean setmealUpdateStatusById(Integer status ,String id){

        String[] split = id.split(",");

        int count = 0;
        for (String s : split) {

            int i = setmealMapper.setmealUpdateStatusById(status,s);
            count=count+i;
        }

        return count == split.length;
    }

}
